<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/12 0012
 * Time: 21:52
 */
//客户端代码
$address = '127.0.0.1';
$port = 9800;
$client_socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die('socket_create 失败原因是 ' . socket_strerror(socket_last_error()) . "\n");

socket_connect($client_socket, $address, $port) or die("Could not connet server\n");    //  连接 成功时返回 TRUE， 或者在失败时返回 FALSE

socket_write($client_socket, "hello socket") or die('write fail' . "\n"); //返回成功写入套接字或者在失败时返回的字节数。FALSE
//PHP_BINARY_READ - 使用系统recv（）函数。用于读取二进制数据的安全
while ($buffer = socket_read($client_socket, 1024, PHP_BINARY_READ)) {
    echo("Response was:" . $buffer . "\n");
}

socket_close($client_socket);